<template>
  <a-card :bordered="false">
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 收款合同
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:scrollX}"
        :components="draggableComponent"
        bordered
        rowKey="id"
        :columns="columns1"
        :dataSource="dataSource1"
        :loading="loading1"
        class="j-table-force-nowrap"
        >
        <a slot="contractName" slot-scope="text, record" @click="getContractDetail(record.id)">{{ text }}</a>
        <a slot="changeNum" slot-scope="text, record" @click="getContractList(record.id)">{{ text }}</a>
        <a slot="invoiceCountAmount" slot-scope="text, record" @click="onInvoiceDetail(record.id)">{{ text }}</a>
        <a slot="realCollectionPaymentMoney" slot-scope="text, record" @click="onPaymentDetail(record.id)">{{ text }}</a>
        <template slot="status" slot-scope="status">
          <a-badge :status="status | statusTypeFilter" :text="status | statusFilter"/>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
      </a-table>
    </div>
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 付款合同
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:scrollXOther}"
        bordered
        :components="draggableComponentOther"
        rowKey="id"
        :columns="columns2"
        :dataSource="dataSource2"
        :loading="loading2"
        class="j-table-force-nowrap"
        >
        <a slot="contractName" slot-scope="text, record" @click="getDetail(record.id)">{{ text }}</a>
        <a slot="reimbursementAmount" slot-scope="text, record" @click="getReimbursementDetail(record)">{{ text }}</a>
        <a slot="realCollectionPaymentMoney" slot-scope="text, record" @click="getPaymentDetail(record)">{{ text }}</a>
        <template slot="status" slot-scope="status">
          <a-badge :status="status | statusTypeFilter" :text="status | statusFilter"/>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
      </a-table>
    </div>
    <ErpProjectFinancePaymentListPenetrate ref="financePaymentList"></ErpProjectFinancePaymentListPenetrate> <!-- 付款合同 已支付金额 -->
    <ErpProjectFinanceReimbursementListPenetrate ref="financeReimbursementList"></ErpProjectFinanceReimbursementListPenetrate> <!-- 付款合同 已报销金额 -->
    <ErpFinanceCollectMoneyBillListPenetrate ref="collectMoneyBill"></ErpFinanceCollectMoneyBillListPenetrate><!-- 收款合同 已收金额 -->
    <ErpFinanceInvoiceBillListPenetrate ref="invoiceBill"></ErpFinanceInvoiceBillListPenetrate> <!-- 收款合同 开票金额 -->
    <erp-project-contract-manager-final-form ref="contractForm" ></erp-project-contract-manager-final-form> <!-- 付款合同名称 -->
    
    <a-modal
      :maskClosable="false"
      title="合同详情"
      :width="1350"
      :bodyStyle="{
        padding: '0 24px'
      }"
      :visible="contractVisible"
      @cancel="handleCancel"
      cancelText="关闭">
      <a-card
        :bordered="false"
        :bodyStyle="{
          padding: '10px 24px'
        }">
        <ErpOmcContractManagerFinalForm ref="contractManageForm" :disabled="true" normal></ErpOmcContractManagerFinalForm> <!-- 收款合同 -->
      </a-card>
      <template slot="footer">
        <a-button @click.stop="()=>{contractVisible=false}">关闭</a-button>
      </template>
    </a-modal>
    <erp-project-contract-manager-list ref="contractList" ></erp-project-contract-manager-list> <!-- 收款合同 变更次数 -->
  </a-card>
</template>

<script>
import Ellipsis from '@/components/Ellipsis'
import { getAction } from '@/api/manage';
import ErpProjectContractManagerFinalForm from './ErpProjectContractManagerFinalForm'
import ErpOmcContractManagerFinalForm from '@/views/erp/omc/contractmanager/modules/ErpOmcContractManagerFinalForm'
import ErpProjectContractManagerList from './ErpProjectContractManagerList'
import ErpFinanceInvoiceBillListPenetrate from '@/views/scy/penetrate/ErpFinanceInvoiceBillListPenetrate'
import ErpFinanceCollectMoneyBillListPenetrate from '@/views/scy/penetrate/ErpFinanceCollectMoneyBillListPenetrate'
import ErpProjectFinanceReimbursementListPenetrate from '@/views/scy/penetrate/ErpProjectFinanceReimbursementListPenetrate'
import ErpProjectFinancePaymentListPenetrate from '@/views/scy/penetrate/ErpProjectFinancePaymentListPenetrate'
import { DragResizable } from '@/mixins/DragResizable'

export default {
  name: 'ErpProjectContractReportForm',
  mixins: [ DragResizable ],
  components: {
    Ellipsis,
    ErpProjectFinancePaymentListPenetrate,
    ErpProjectFinanceReimbursementListPenetrate,
    ErpFinanceCollectMoneyBillListPenetrate,
    ErpFinanceInvoiceBillListPenetrate,
    ErpProjectContractManagerFinalForm,
    ErpProjectContractManagerList,
    ErpOmcContractManagerFinalForm
  },
  watch: {
    projectId: {
      handler (val) {
        this.loadData(val)
      },
      immediate: true
    }
  },
  props: {
    //项目id
    projectId: {
      type: String,
      default: 'undefined',
      required: false
    }
  },
  data() {
    return {
      contractVisible: false,
      /* 排序参数 */
      isorter: {
        column: 'createTime',
        order: 'desc',
      },
      url: {
        list: '/api/erp/project/report/erpProjectContractReport/list'
      },
      loading1: false,
      loading2: false,
      dataSource1: [],
      dataSource2: [],
      columns1: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '收款合同',
          align: 'center',
          dataIndex: 'contractName',
          scopedSlots: { customRender: 'contractName' },
          width: 214,
          ellipsis: true
        },
        {
          title: '变更记录次数',
          align: 'center',
          dataIndex: 'changeNum',
          scopedSlots: { customRender: 'changeNum' },
          width: 214,
          ellipsis: true
        },
        {
          title: '收款签订日期',
          align: 'center',
          dataIndex: 'signDate',
          width: 214,
          ellipsis: true
        },
        {
          title: '合同金额（元）',
          align: 'center',
          dataIndex: 'contractMoney',
          width: 214,
          ellipsis: true
        },
        {
          title: '开票金额（元）',
          align: 'center',
          dataIndex: 'invoiceCountAmount',
          scopedSlots: { customRender: 'invoiceCountAmount' },
          width: 214,
          ellipsis: true
        },
        {
          title: '已收金额（元）',
          align: 'center',
          dataIndex: 'realCollectionPaymentMoney',
          scopedSlots: { customRender: 'realCollectionPaymentMoney' },
          width: 214,
          ellipsis: true
        },
        {
          title: '收款比例（%）',
          align: 'center',
          dataIndex: 'collectPaymentRate',
          width: 214,
          ellipsis: true
        },
        {
          title: '未收金额（元）',
          align: 'center',
          ellipsis: true,
          width: 214,
          dataIndex: 'noCollectionPaymentMoney'
        }
      ],
      columns2: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          fixed: 'left',
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '付款合同',
          align: 'center',
          dataIndex: 'contractName',
          scopedSlots: { customRender: 'contractName' },
          width: 244,
          ellipsis: true
        },
        {
          title: '付款签订日期',
          align: 'center',
          dataIndex: 'signDate',
          width: 244,
          ellipsis: true
        },
        {
          title: '合同金额（元）',
          align: 'center',
          dataIndex: 'contractMoney',
          width: 244,
          ellipsis: true
        },
        {
          title: '已报销金额（元）',
          align: 'center',
          dataIndex: 'reimbursementAmount',
          scopedSlots: { customRender: 'reimbursementAmount' },
          width: 244,
          ellipsis: true

        },
        {
          title: '已支付金额（元）',
          align: 'center',
          dataIndex: 'realCollectionPaymentMoney',
          scopedSlots: { customRender: 'realCollectionPaymentMoney' },
          width: 244,
          ellipsis: true
        },
        {
          title: '付款比例(%)',
          align: 'center',
          dataIndex: 'collectPaymentRate',
          width: 244,
          ellipsis: true
        },
        {
          title: '未付金额（元）',
          align: 'center',
          dataIndex: 'noCollectionPaymentMoney',
          width: 244,
          ellipsis: true
        }
      ]
    }
  },

  computed: {
  },
  created() {
    this.customKey = 'columns1'
    this.otherKey = 'columns2'
    this.columns1width = this.columns1.map(val => {
      return val.width
    })
    this.columns2width = this.columns2.map(val => {
      return val.width
    })
  },
  mounted() {
  },
  methods: {
    clearDeail(val) {
      this.dataSource2 = []
      this.dataSource1 = []
      this.dataSource = []
      this.projectId = val
      this.loadData()
    },
    getPaymentDetail(record){
      //先清空数据
      this.$refs.financePaymentList.dataSource = []
      this.$refs.financePaymentList.ipaginationss.total = 0
      //查询数据
      this.$refs.financePaymentList.loadingss = true
      this.$refs.financePaymentList.activeKey = '1'
      this.$refs.financePaymentList.contractId = record.id
      this.$refs.financePaymentList.searchResetss()
        //打开弹窗
      this.$refs.financePaymentList.visible = true
    },
    getReimbursementDetail(record){
      //先清空数据
      this.$refs.financeReimbursementList.dataSource = []
      this.$refs.financeReimbursementList.ipaginations.total = 0
      //查询数据
      this.$refs.financeReimbursementList.loadings = true
      this.$refs.financeReimbursementList.activeKey = '1'
      this.$refs.financeReimbursementList.contractId = record.id
      this.$refs.financeReimbursementList.searchResets()
        //打开弹窗
      this.$refs.financeReimbursementList.visible = true
    },
    onPaymentDetail(id){
      //先清空数据
      this.$refs.collectMoneyBill.dataSource = []
      this.$refs.collectMoneyBill.ipagination.total = 0
      //查询数据
      this.$refs.collectMoneyBill.loading = true
      this.$refs.collectMoneyBill.contractNo = id
      this.$refs.collectMoneyBill.searchReset()
      //打开弹窗
      this.$refs.collectMoneyBill.visible = true
    },
    onInvoiceDetail(id){
      //先清空数据
      this.$refs.invoiceBill.dataSource = []
      this.$refs.invoiceBill.ipagination.total = 0
      //查询数据
      this.$refs.invoiceBill.loading = true
      this.$refs.invoiceBill.contractId = id
      this.$refs.invoiceBill.searchReset()
      //打开弹窗
      this.$refs.invoiceBill.visible = true
    },
    getContractDetail(id) {
      getAction('/api/erp/omc/contractmanager/erpOmcContractManagerFinal/queryById', { id }).then((res) => {
        if (res.success) {
          const model = res.result
          this.contractVisible = true
          this.$nextTick(() => {
            this.$refs.contractManageForm.edit(model);
          })
          // this.model = model;
        }
      })
    },
    handleCancel() {
      this.contractVisible = false;
    },
    getDetail(val) {
      this.$refs.contractForm.initData(val);
      this.$refs.contractForm.visible = true;
    },
    getContractList(val) {
      this.$refs.contractList.initData(val);
      this.$refs.contractList.carId=val;
      this.$refs.contractList.visible = true;
    },
    async loadData(arg) {
      if (!this.url.list) {
        this.$message.error("请设置url.list属性!")
        return
      }
      // 加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      let params = {
        projectId: this.projectId,
        pageSize: 100
      };
      // 查询条件
      var param = Object.assign({}, params, this.isorter, {});
      this.loading1 = true;
      this.loading2 = true;
      let res = await getAction(this.url.list, param)
      this.loading1 = false;
      this.loading2 = false;
      if (res.success) {
        this.dataSource = res.result.records;
        if (this.dataSource.length > 0) {
          // 收款合同
          this.dataSource1 = this.dataSource.filter(item => {
            return item.contractType === '1'
          })
          // 时间降序排列
          if (this.dataSource1.length > 0) {
            this.dataSource1 = this.dataSource1.sort((a, b) => new Date(a.createTime).getTime() - new Date(b.createTime).getTime()).reverse();
          }
          // 付款合同
          this.dataSource2 = this.dataSource.filter(item => {
            return item.contractType === '2'
          })
          // 时间降序排列
          if (this.dataSource2.length > 0) {
            this.dataSource2 = this.dataSource2.sort((a, b) => new Date(a.createTime).getTime() - new Date(b.createTime).getTime()).reverse();
          }
        }
      }
      this[this.customKey].forEach((val,index) => {
        val.width = this[this.customKey+'width'][index]
      })
      this[this.otherKey].forEach((val,index) => {
        val.width = this[this.otherKey+'width'][index]
      })
    }
  }
}
</script>
<style scoped>
</style>
